import { Http } from "../../utils/http";
import { cache } from "../../utils/util";
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    scene: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
//     this.setData({scene: options.scene})
//     app.getToken( (token) => {
     
//     })
// return;
    this.setData({
      scene: options.scene,
    })
    let init = await this.init_();
    // console.log(init);


    if (init.code == 0) {
      let name = init.data.product_name;
      wx.showModal({
        title: '是否确定核销 ' + name  + ' ?',
        // content: '内容描述',
        success: (res) => {
          if (res.confirm) {
            // wx.showLoading({
            //   title: '正在核销中...',
            // })
            console.log(app.config);
            wx.request({
              
              url: app.Url + '/wechat/order/verify',
              data: {
                order_no: this.data.scene,
              },
              header: {
                token: wx.getStorageSync('token'),
              },
              success: (res) => {
                console.log(res);
                if (res.data.code == 0) {
                  // console.log('Hello');
                  wx.showToast({
                    title: res.data.msg,
                    duration: 3000,
                    success: (res) => {
                      setTimeout(function () {
                        wx.navigateTo({
                          url: '/pages/ordercenter/index',
                        })
                      }, 3000)
                    },
                  })
                } else {
                  wx.showToast({
                    title: res.data.msg,
                    duration: 3000,
                    success: (res) => {
                      setTimeout(() => {
                        wx.navigateTo({
                          url: '/pages/ordercenter/index',
                        })
                      }, 3000);
                    },
                  })
                }


              },
              fail: (res) => {

              },
            });

            // setTimeout(function () {
            //   wx.hideLoading({})
            //   wx.showToast({
            //     title: '核销成功',
            //     duration: 2500,
            //   })
            // }, 3000)


          } else if (res.cancel) {
            wx.showToast({
              title: '核销取消',
              icon: 'error',
              duration: 2500,
              success: (res) => {
                setTimeout(function () {
                  wx.navigateTo({
                    url: '/pages/ordercenter/index',
                  })
                }, 3000)
              },
            })
          }
        },
      })
    } else {
      wx.showModal({
        title: '核销失败',
        content: init.msg,
        showCancel: false,
      })
    }
  },

  async init_() {
    return await Http.request({
      url: 'wechat/order/verify_validate',
      data: { order_no: this.data.scene }
    })
  }

})